<?php

namespace App\Http\Controllers\Admin;

use App\Models\WechatMenus;
use App\Models\Setting;
use Exception;
use App\Support\Wechat\Menu;
use App\Support\Wechat\MenuItem;

class WechatMenusController extends BaseController
{
    public function __construct(WechatMenus $menus, Setting $setting)
    {
        parent::__construct();

        $this->menus = $menus;
        $this->wechatSet = $setting;
    }

    public function getIndex()
    {
        $where['pid'] = 0;
        $data = $this->menus->getList($where, '', ['sort' => 'asc']);
        if (count($data) > 0) {
            foreach ($data as $d) {
                $d->sub = $this->menus->getList(['pid' => $d->id], '', ['sort' => 'asc']);
            }
        }
        return view('admin.diymenus.index', compact('data'));
    }

    public function getCreate()
    {
        $where['pid'] = 0;
        $menus = $this->menus->getList($where);
        return view('admin.diymenus.edit', compact('menus'));
    }


    public function postCreate()
    {
        $request = request();
        $data = $request->except('_token');
        $nums = $this->menus->getNums(['pid' => $data['pid']]);
        if ($data['pid'] == 0 && $nums >= 3) {
            return redirect()->back()->with('message', '顶级菜单最多添加3个！')->withInput();
        }
        if ($data['pid'] > 0 && $nums >= 5) {
            return redirect()->back()->with('message', '子菜单最多添加5个！')->withInput();
        }
        $data['status'] = isset($data['status']) ? 1 : 0;
        $res = $this->menus->add($data);
        if (!$res) {
            return redirect()->back()->with('message', '添加失败！')->withInput();
        }
        return redirect()->back()->with('message', '添加成功！');
    }


    public function getEdit($id)
    {
        $data = $this->menus->getOne(['id' => $id]);
        $menus = $this->menus->getList(['pid' => 0]);
        return view('admin.diymenus.edit', compact('data', 'menus'));
    }


    public function postEdit()
    {
        $request = request();
        $id = $request['id'];
        $data = $request->except('_token', 'id');
        $where['pid'] = $data['pid'];
        $where['id'] = ['<>', $id];
        $nums = $this->menus->getNums($where);
        if ($data['pid'] == 0 && $nums >= 3) {
            return redirect()->back()->with('message', '顶级菜单最多添加3个！')->withInput();
        }
        if ($data['pid'] > 0 && $nums >= 5) {
            return redirect()->back()->with('message', '子菜单最多添加5个！')->withInput();
        }
        $data['status'] = isset($data['status']) ? 1 : 0;
        $res = $this->menus->up(['id' => $id], $data);
        if (!$res) {
            return redirect()->back()->with('message', '编辑失败！')->withInput();
        }
        return redirect()->back()->with('message', '编辑成功！')->withInput();
    }

    public function getDestroy($id)
    {
        $res = $this->menus->del(['id' => $id]);
        $ajaxData['status'] = 0;
        $ajaxData['msg'] = '删除失败！';
        if ($res) {
            $ajaxData['status'] = 1;
            $ajaxData['msg'] = '删除成功！';
        }
        return ajax_return($ajaxData);
    }

    public function getMakeMenu()
    {

        $where['pid'] = 0;
        $where['status'] = 1;
        $data = $this->menus->getList($where, '', ['sort' => 'asc']);
        if (count($data) > 0) {
            foreach ($data as $d) {
                $d->sub = $this->menus->getList(['pid' => $d->id, 'status' => 1], '', ['sort' => 'asc']);
            }
        }
        $res = $this->setMenu($data);
        $ajaxData['status'] = $res ? 1 : 0;
        $ajaxData['msg'] = $res ? '生成菜单成功' : '生成菜单失败';
        return ajax_return($ajaxData);
    }

    public function setMenu($data)
    {
        $menu = array();
        foreach ($data as $k => $v) {
            if (count($v['sub'])) {
                $array = new MenuItem($v['name']);
                $buttons = array();
                foreach ($v['sub'] as $key => $value) {
                    if ($value['type'] == 'view') {
                        $buttons[] = new MenuItem($value['name'], $value['type'], $value['url']);
                    } else {
                        $buttons[] = new MenuItem($value['name'], $value['type'], $value['key']);
                    }
                }
                $menu[] = $array->buttons($buttons);
            } else {
                if ($v['type'] == 'view') {
                    $array = new MenuItem($v['name'], $v['type'], $v['url']);
                } else {
                    $array = new MenuItem($v['name'], $v['type'], $v['key']);
                }
                $menu[] = $array;
            }
        }
        $wechat = $this->wechatSet->getSetting('wechat');
        $menuService = new Menu($wechat['app_id'], $wechat['app_secret']);
        try {
            $result = $menuService->set($menu);// 请求微信服务器
            return $result;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }  
}
